// CherryMerry ( Mikhail Tomilenko ), Reverse Entertainment, 2021-2024


#include "../Public/NF_GameInstance.h"
#include "../Public/NF_Character.h"
#include <NifestGame/Interfaces/Public/I_Core.h>

#include "Kismet/GameplayStatics.h"

void UNF_GameInstance::CreateNewGame()
{
	APlayerController* PlayerController = GetFirstLocalPlayerController();

	PlayerController->bShowMouseCursor = false;
	FInputModeGameOnly InputGameOnlyModeData;
	PlayerController->SetInputMode(InputGameOnlyModeData);
	InitType = EStartGameType::SGTE_NewGame;
}

void UNF_GameInstance::LoadGame()
{
	APlayerController* PlayerController = GetFirstLocalPlayerController();

	PlayerController->bShowMouseCursor = false;
	FInputModeGameOnly InputGameOnlyModeData;
	PlayerController->SetInputMode(InputGameOnlyModeData);
	InitType = EStartGameType::SGTE_LoadGame;
}

void UNF_GameInstance::LevelOpened_Implementation()
{
	II_Core* CoreInterface = Cast<II_Core>(GetFirstLocalPlayerController());

	switch (InitType)
	{
	case EStartGameType::SGTE_NewGame:
		UE_LOG(LogTemp, Warning, TEXT("SGTE_NewGame"))
		if (CoreInterface)
		{
			UE_LOG(LogTemp, Warning, TEXT("Interface is valid, creating"))
			CoreInterface->Execute_InitializeNewGame(GetFirstLocalPlayerController());
		}
	case EStartGameType::SGTE_LoadGame:
		UE_LOG(LogTemp, Warning, TEXT("SGTE_LoadGame"))
		if (CoreInterface)
		{
			UE_LOG(LogTemp, Warning, TEXT("Interface is valid, loading"))
			CoreInterface->Execute_InitializeLoadGame(GetFirstLocalPlayerController());
		}
	}
}

void UNF_GameInstance::LevelLoaded_Implementation()
{
}

void UNF_GameInstance::StartGame(EStartGameType SelectedInitType)
{
	UGameplayStatics::OpenLevel(this, "TestMap");

	switch(SelectedInitType)
	{
	case EStartGameType::SGTE_NewGame:
		CreateNewGame();
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Creating new game..."));
		break;
	case EStartGameType::SGTE_LoadGame:
		LoadGame();
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Loading game from selected save slot..."));
		break;
	}
}
